﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2017-20XX                                                    *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2019/7/4 11:00:00
 *  文件描述:宏开关类
 *****************************************************************************/
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Util
{
    /* 使用模版
    [InitializeOnLoad]
    public class CompilingCodeEditor
    {
        static CompilingCodeEditor()
        {
            MacroSwitch.Add("isCompilingCode");
        }
    }
    */
    public class MacroSwitch : EditorWindow
    {
        private static MacroSwitch win;
        public static Dictionary<string, bool> AllMacroSwitch = new Dictionary<string, bool>();
        [MenuItem("Tools/宏开关")]
        public static void Init()
        {
            win = GetWindow<MacroSwitch>("MacroSwitch");
            win.position = new Rect(700, 80, 200, AllMacroSwitch.Count * 30 + 30);
            win.init();
            win.Show();
        }
        //添加宏
        public static void Add(string Name)
        {
            if (!AllMacroSwitch.ContainsKey(Name))
            {
                AllMacroSwitch.Add(Name, true);
            }
        }
        //界面初始化
        public void init()
        {
            var e = new List<string>(AllMacroSwitch.Keys);
            for (int i = 0; i < e.Count; i++)
            {
                var Name = e[i];
                AllMacroSwitch[Name] = false;
                if (Macro.Contains(Name))
                {
                    AllMacroSwitch[Name] = true;
                }
            }
        }

        private void OnGUI()
        {
            var e = new List<string>(AllMacroSwitch.Keys);
            for (int i = 0; i < e.Count; i++)
            {
                var Name = e[i];
                EditorGUILayout.BeginHorizontal();
                var isUseMonobot = AllMacroSwitch[Name];
                GUILayout.Label(Name);
                if (isUseMonobot)
                {
                    if (GUILayout.Button("关闭"))
                    {
                        AllMacroSwitch[Name] = false;
                        Macro.SetEnabled(Name, false);
                        init();
                    }
                }
                else
                {
                    if (GUILayout.Button("开启"))
                    {
                        AllMacroSwitch[Name] = true;
                        Macro.SetEnabled(Name, true);
                        init();
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
        }
    }
    public class Macro
    {
        public static BuildTargetGroup[] buildTargetGroups = new BuildTargetGroup[]
        {
            BuildTargetGroup.Standalone,
            BuildTargetGroup.iOS,
        };
        //设置宏开关
        public static void SetEnabled(string defineName, bool enable)
        {
            foreach (var group in buildTargetGroups)
            {
                var defines = GetDefinesList(group);
                if (enable)
                {
                    if (defines.Contains(defineName))
                    {
                        return;
                    }
                    defines.Add(defineName);
                }
                else
                {
                    if (!defines.Contains(defineName))
                    {
                        return;
                    }
                    while (defines.Contains(defineName))
                    {
                        defines.Remove(defineName);
                    }
                }
                string definesString = string.Join(";", defines.ToArray());
                PlayerSettings.SetScriptingDefineSymbolsForGroup(group, definesString);
            }
        }


        public static List<string> GetDefinesList()
        {
            return GetDefinesList(buildTargetGroups[0]);
        }
        //判断当前宏是否开启
        public static bool Contains(string defineName)
        {
            var defines = GetDefinesList();
            if (defines.Contains("isCompilingCode"))
            {
                return true;
            }
            return false;
        }

        public static List<string> GetDefinesList(BuildTargetGroup group)
        {
            return new List<string>(PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';'));
        }
    }
}

